<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python 流程控制- if 条件语句 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python 流程控制- if 条件语句</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 条件语句是通过一条或多条语句的执行结果（true 或者 false）来决定执行的代码块。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过下图来简单了解条件语句的执行过程：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5/01.jpg?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 程序语言指定任何非0或非空（null）值为true，0或者null为false。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 编程中 if 语句用于控制程序的执行，基本形式为：</p>
<ul>
<li>if 语句</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> expression（判断条件）:      </div><div class="line">    statement(s)（执行语句……）</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    statement(s)（执行语句……）</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：Python 使用缩进作为其语句分组的方法，建议使用4个空格。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中“判断条件”成立时（非零），则执行后面的语句，而执行内容可以多行，以缩进来区分表示统一范围。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;else 为可选语句，放需要在条件不成立时，执行内容则可以执行相关语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line"><span class="comment"># 例1：if 基本用法</span></div><div class="line"> </div><div class="line">flag = <span class="keyword">False</span></div><div class="line">name = <span class="string">'luren'</span></div><div class="line"><span class="keyword">if</span> name == <span class="string">'python'</span>:         <span class="comment"># 判断变量否为'python'</span></div><div class="line">    flag = <span class="keyword">True</span>          <span class="comment"># 条件成立时设置标志为真</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'welcome boss'</span>    <span class="comment"># 并输出欢迎信息</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> name              <span class="comment"># 条件不成立时输出变量名称</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">luren  <span class="comment">#输出结果</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if 语句的判断条件可以用 &gt;（大于）、&lt;（小于）、==（等于）、&gt;=（大于等于）、&lt;=（小于等于）来表示其关系。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当判断条件为多个值时，可以用以下形式：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> expression1（判断条件<span class="number">1</span>）:      </div><div class="line">    statement1(s)（执行语句<span class="number">1</span>……）</div><div class="line"><span class="keyword">elif</span> expression2（判断条件<span class="number">2</span>）:      </div><div class="line">    statement2(s)（执行语句<span class="number">2</span>……）</div><div class="line"><span class="keyword">elif</span> expression3（判断条件<span class="number">3</span>）:      </div><div class="line">    statement3(s)（执行语句<span class="number">3</span>……）</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    statement(s)（执行语句……）</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实例如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"><span class="comment"># 例2：elif用法</span></div><div class="line"> </div><div class="line">num = <span class="number">5</span>     </div><div class="line"><span class="keyword">if</span> num == <span class="number">3</span>:            <span class="comment"># 判断num的值</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'boss'</span>        </div><div class="line"><span class="keyword">elif</span> num == <span class="number">2</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'user'</span></div><div class="line"><span class="keyword">elif</span> num == <span class="number">1</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'worker'</span></div><div class="line"><span class="keyword">elif</span> num &lt; <span class="number">0</span>:           <span class="comment"># 值小于零时输出</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'error'</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'roadman'</span>     <span class="comment"># 条件均不成立时输出</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果为：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">roadman    <span class="comment">#输出结果</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于 python 并不支持 switch 语句，多疑多个条件判断，只能用 elif 来实现，如果判断需要多个条件同时判断时，可以使用 or（或），表示两个条件有一个成立时判断条件成功；使用 and（与）时，表示只有两个条件同时成立的情况下，判断条件才成功。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line"><span class="comment"># 例3：if语句多个条件</span></div><div class="line"> </div><div class="line">num = <span class="number">9</span></div><div class="line"><span class="keyword">if</span> num &gt;= <span class="number">0</span> <span class="keyword">and</span> num &lt;= <span class="number">10</span>:    <span class="comment"># 判断值是否在0~10之间</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'hello'</span></div><div class="line"><span class="comment"># 输出结果: hello</span></div><div class="line"> </div><div class="line">num = <span class="number">10</span></div><div class="line"><span class="keyword">if</span> num &lt; <span class="number">0</span> <span class="keyword">or</span> num &gt; <span class="number">10</span>:    <span class="comment"># 判断值是否在小于0或大于10</span></div><div class="line">    <span class="keyword">print</span> <span class="string">'hello'</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'undefine'</span></div><div class="line"><span class="comment"># 输出结果: undefine</span></div><div class="line"> </div><div class="line">num = <span class="number">8</span></div><div class="line"><span class="comment"># 判断值是否在0~5或者10~15之间</span></div><div class="line"><span class="keyword">if</span> (num &gt;= <span class="number">0</span> <span class="keyword">and</span> num &lt;= <span class="number">5</span>) <span class="keyword">or</span> (num &gt;= <span class="number">10</span> <span class="keyword">and</span> num &lt;= <span class="number">15</span>):    </div><div class="line">    <span class="keyword">print</span> <span class="string">'hello'</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">print</span> <span class="string">'undefine'</span></div><div class="line"><span class="comment"># 输出结果: undefine</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当 if 有多个条件时可使用括号来区分判断的先后顺序，括号中的判断优先执行，此外 and 和 or 的优先级低于 &gt;(大于)、&lt;（小于）等判断符号，即大于和小于在没有括号的情况下会比与要优先判断。</p>
<h2 id="简单的语句组"><a href="#简单的语句组" class="headerlink" title="简单的语句组"></a>简单的语句组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以在同一行的位置上使用 if 条件判断语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python </span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"> </div><div class="line">var = <span class="number">100</span> </div><div class="line"> </div><div class="line"><span class="keyword">if</span> ( var  == <span class="number">100</span> ) : <span class="keyword">print</span> <span class="string">"变量 var 的值为100"</span> </div><div class="line"> </div><div class="line"><span class="keyword">print</span> <span class="string">"Good bye!"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行输出结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">变量 var 的值为 <span class="number">100</span></div><div class="line">Good bye！</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/2.0 2.1 流程控制-if条件/12. Python 流程控制-if 条件语句/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
